var app = getApp();
var util = require('../../utils/util.js');
var api = require('../../config/api.js');
// var needList = ['OUTDOOR', 'SCENIC', 'TEAM_BUILDING'];
Page({
  data: {
    showBottom: false,
    loadMoreData: '~到底了喔~',
    recommendList: [],
    iconList: [],
    bannerList: [],
    placeholder: "点击输入目的地/关键词",
    word: '',
    loadingHidden: true,//loadingHidden: false, //水哥调试注释
    endCampaginList:[]

  },
  pageSize: 2,
  currentPageNum: 0,
  recommend_typeid:1,

  onLoad: function() {

    
    let _this = this;
    _this.getBannerList();
    _this.getActivityClassify();
    _this.getRecommendList();
    _this.getEndCampaginList();
    

     
  },
  onReady:function(){
    
  },
  onShow: function() {
  },
  /**
   * banner 点击事件
  */
  changeSwiper: function(e) {

    console.log(e)
    let _this = this;
    let dataset = e.currentTarget.dataset.itemlist;

    var url = dataset.jumpUrl;
    util.request(api.ActivityUrl, dataset.jumpParams, dataset.jumpMethod).then(res => {
      console.log(res,8888)
      app.globalData.bannerGoodLists = res.responseBody.campaignList
    })

    wx.navigateTo({
      url: dataset.jumpPage + '?type=BANNER&key=' + dataset.key,
    })
  },

  /**
   * 获取首页banner列表
  */
  getBannerList: function() {
    let that = this;
    util.request(api.ConfRequest, {
      type: 'INDEX_BANNER'
    }).then(function(res) {
      var tmplist = res.responseBody
      that.setData({
        bannerList: tmplist
      })
    })
  },
  /**
   * 获取首页分类豆腐块
   */
  getActivityClassify: function() {
    let that = this;
    util.request(api.ActivityClassify).then(function(res) {
      if (res.code == 200) {
        console.log(res.responseBody)
        var tmplist = res.responseBody
        var iconList = [];
        for (var index in tmplist) {
          var icon = {};
          icon.fullName = tmplist[index].fullName;
          icon.imgUrl = tmplist[index].imgUrl;
          icon.id = tmplist[index].id;
          icon.name = tmplist[index].name;
          // if (needList.indexOf(tmplist[index].name) > -1) {
          //   iconList.push(icon)
          // }
          iconList.push(icon)
        }
        that.setData({
          iconList: iconList
        })
      }
    })
  },
  /**
   * 点击豆腐块分类事件
  */
  getActivityByType: function(e) {

    console.log(e)
    var name = e.currentTarget.dataset.type.fullName
    if (name == '定制游'){
      wx.navigateTo({
        url: '/pages/customizeActivity/companyCustomize/companyCustomize',
      })
    }else{
      wx.navigateTo({
        url: '/pages/catalog/catalog?typeId=' + e.currentTarget.dataset.type.id + '&fullName=' + e.currentTarget.dataset.type.fullName
      })
    }


    
  },
  /**
   * 获取首页推荐列表
   */
  getRecommendList: function(e) {

    var that = this;
    var data = {}
    data.pageSize = that.pageSize
    util.request(api.RecommendList, data).then(function(success) {
      var recommendList = success.responseBody
      that.recommend_typeid = recommendList.length >= 1 ? recommendList[0].type ? recommendList[0].type : 1 : 1
      that.setData({
        recommendList: recommendList
      });
    }, function(error) {
      console.log('获取推荐列表失败')
    })

  },
  /**
   * 瀑布流商品点击事件
  */
  toRecommendDetail: function(e) {
    var that = this;
    app.currentGoodsId = e.currentTarget.dataset.index
    wx.navigateTo({
      url: '/pages/goods/goods?id=' + app.currentGoodsId
    })
  },

  getEndCampaginList:function(e){
    var that = this;
    var data={}
    data.pageSize = that.pageSize
    util.request(api.EndCampaginList,data).then(function (success) {
      var endCampaginList = success.responseBody.campaignList

      console.log(success)
      that.setData({
        endCampaginList: endCampaginList,
        loadingHidden: true
      });
    }, function (error) {
      console.log('获取推荐列表失败')
    })

  },
  //推荐商品的查看更多
  recommend_more:function(e){
    
    wx.navigateTo({
      url: '/pages/catalog/catalog?typeId=' + this.recommend_typeid + '&fullName=热销活动'
    })

  },
  //往期活动的查看更多
  end_more: function (e) {
    wx.navigateTo({
      url: '/pages/endgoods/endgoods',
    })
  },

  /**
   * 监听页面上拉触底事件
   */
  // onReachBottom: function() {
  //   console.log("触底了~~~")
  //   var that = this;
  //   var nowPageSize = that.pageSize + 5
  //   var currentPage = that.currentPageNum

  //   if (!that.data.showBottom) {
  //     wx.showLoading({
  //       title: '玩命加载中',
  //     })
  //     util.request(api.RecommendList, {
  //       pageSize: nowPageSize
  //     }).then(function(success) {

  //       console.log(success)
  //       var recommendList = success.responseBody
  //       //推荐商品加载完毕，显示
  //       if (recommendList.length == that.currentPageNum || recommendList.length < 5) {
  //         that.setData({
  //           showBottom: true
  //         })
  //       }
  //       that.setData({
  //         recommendList: recommendList,
  //       });
  //       that.pageSize = that.pageSize + 5,
  //       that.currentPageNum =  recommendList.length

  //       wx.hideLoading()

  //     }, function(error) {
  //       console.log('获取失败')
  //     })
  //   }
  // }

})